
/**
*----------------------------------------------------------------------------------------
* Here defines all the functions generally used by all menu
*
*/

/**
* Change this row's style
*
* @param  string  id    this row's id
* @param  string  style change to this style
*
*/
function chgStyle(id,style){
	
	document.getElementById(id).className = style;
}

//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){
  
  return(parseFloat(val,10)==(val*1));
}

/**
* Set the background to yellow
*
* @param  String    thisObj
*
*/
function setYellow2(thisObj){
  
  thisObj.style.background= "yellow";
}

/**
* Set the background to white
*
* @param  String    thisObj
*
*/
function setWhite2(thisObj){
  
  thisObj.style.background= "white";
}

/**
* sync the val with 2 parameters take place
*
* @param  integer   srcObject
* @param  String    dstObject
*
*/
function syncVal2(srcObject, dstObject){
  
  srcObject = document.getElementsByName(srcObject);
  srcObject = srcObject[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  /**
  * First check all the source and destinated value
  * That they are numeric and valid
  *
  */
  if(!isNumeric(srcObject.value)){
    
    srcObject.value = 0;
  }
  
  if(!isNumeric(dstObject.value)){
    
    dstObject.value = 0;
  }
  
  /**
  * Sync the val now
  *
  */
  dstObject.value = parseFloat(dstObject.value) - parseFloat(srcObject.value);
}

/**
* sync the val with 2 parameters take place
*
* @param  integer   srcObject
* @param  String    dstObject
*
*/
function syncVal2F(srcObject, dstObject){
  
  srcObject = document.getElementsByName(srcObject);
  srcObject = srcObject[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  /**
  * First check all the source and destinated value
  * That they are numeric and valid
  *
  */
  if(!isNumeric(srcObject.value)){
    
    srcObject.value = 0;
  }
  
  if(!isNumeric(dstObject.value)){
    
    dstObject.value = 0;
  }
  
  /**
  * Sync the val now
  *
  */
  var tmpResult   = parseFloat(dstObject.value) - parseFloat(srcObject.value);
  dstObject.value = tmpResult.toFixed(2);
}

/**
* Sum the val with 2 parameters take place
*
* @param  integer   srcObject
* @param  String    dstObject
*
*/
function sumVal2(srcObject, dstObject){
  
  srcObject = document.getElementsByName(srcObject);
  srcObject = srcObject[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  /**
  * First check all the source and destinated value
  * That they are numeric and valid
  *
  */
  if(!isNumeric(srcObject.value)){
    
    srcObject.value = 0;
  }
  
  if(!isNumeric(dstObject.value)){
    
    dstObject.value = 0;
  }
  
  /**
  * sum the val now
  *
  */
  dstObject.value = parseFloat(dstObject.value) + parseFloat(srcObject.value);
}

/**
* Sum the val with 2 parameters take place
* with floating point, 2 decimal
*
* @param  integer   srcObject
* @param  String    dstObject
*
*/
function sumVal2F(srcObject, dstObject){
  
  srcObject = document.getElementsByName(srcObject);
  srcObject = srcObject[0];
  
  dstObject = document.getElementsByName(dstObject);
  dstObject = dstObject[0];
  
  /**
  * First check all the source and destinated value
  * That they are numeric and valid
  *
  */
  if(!isNumeric(srcObject.value)){
    
    srcObject.value = 0;
  }
  
  if(!isNumeric(dstObject.value)){
    
    dstObject.value = 0;
  }
  
  /**
  * sum the val now
  *
  */
  var tmpResult   = parseFloat(dstObject.value) + parseFloat(srcObject.value);
  dstObject.value = tmpResult.toFixed(2);
}

/**
* set this object with this value
* This function will be used as 
* variable content manipulator
*
* @param  String  dstObject
* @param  String  thisValue
*
*/
function setThisObject(dstObject, thisValue){
  
  dstObject = document.getElementsByName(dstObject);
  
  if(dstObject.length > 0){
    
    dstObject = dstObject[0];
  }
  
  dstObject.value = thisValue;
}

/**
* This is a test Function
* to test if the include is success
*
* @param  void
*
*/
function sayHello(){
  
  alert("Linked GMisc.js Successfuly");
}


var GMisc = {

  /**
  * simple debug
  *
  * @param  HtmlElement   varToInspect
  */
  dbgInfo : function (varToInspect){
  
    alert(varToInspect.name + " : " + varToInspect.value);
  },

  /**
  * It Stricts out NULL to "0"
  *
  * @param  HtmlElement   thisObj
  */
  noNull : function (thisObj){
  
    if(thisObj.value == ""){
    
      thisObj.value = "0";
    }else{
    
      /**
      * Do nothing
      */    
    }
  },
  
  /**
  * Whenever user hit enter 
  * it focus to defined element
  *
  * @param  Window              event
  * @param  CollectionElement   elm
  */
  enterKey : function (event,
                       elm){
    
    if(event.which){
      
      keynum = event.which;
      
      if(keynum == 13){
    
        elm = document.getElementsByName(elm);
        
        elm = this.firstElement(elm);
        
        elm.focus();
      }
    }
  },
  
  isArray : function (obj) {
	   
    if(obj.constructor.toString().indexOf("Array") == -1){
      
      return false;
    }else{
      
      return true;
    }
	},
  
  /**
  * getElementsByName helper
  * To fetch the 1st element
  * 
  * @param string  elm   the Element' name
  * @return string
  */
  firstElement : function (elm){
    
    if(elm.length > 0){
    
      return elm[0];
    }else{
    
      return elm;
    }
  },
  
  /********************************************************
	 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
	 * CodeHouse(TM) is a registered trademark.
	 *
	 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
	 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
	 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
	 * 
	 * CodeHouse.com JavaScript Library Module: Get Current Style Method
	 *
	 * You can obtain this script at http://www.codehouse.com
	 ********************************************************/
  getCurrentStyle : function (elem, prop){
	
	  //elem = document.getElementsByName(elem);
	  
	  //elem = this.firstElement(elem);
	
	  /**
	  * IE
	  */
		if( elem.currentStyle ){  
	   
	    var ar = prop.match(/\w[^-]*/g);
	    var s = ar[0];
	   
	    for(var i = 1; i < ar.length; ++i){
	    
	      s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
	    }
	        
	   return elem.currentStyle[s]
		}
		/**
		* Mozilla , Opera, ...
		*/
		else 
		if( document.defaultView.getComputedStyle ){
		
		  return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
		}
  },
  
  /**
	* Change this row's style
	*
	* @param  string  id    this row's id
	* @param  string  style change to this style
	*
	*/
	chgStyle : function (id,style){
	  
	  document.getElementById(id).className = style;
	},
	
	//-------------------------------------------------------------------
	// isNumeric(value)
	//   Returns true if value contains a positive float value
	//-------------------------------------------------------------------
	isNumeric : function (val){
	  
	  return(parseFloat(val,10)==(val*1));
	},
	
	/**
	* Set the background to yellow
	*
	* @param  String    thisObj
	*
	*/
	setYellow2 : function (thisObj){
	  
	  thisObj.style.background= "yellow";
	},
	
	/**
	* Set the background to white
	*
	* @param  String    thisObj
	*
	*/
	setWhite2 : function (thisObj){
	  
	  thisObj.style.background= "white";
	},
	
	/**
	* sync the val with 2 parameters take place
	*
	* @param  integer   srcObject
	* @param  String    dstObject
	*
	*/
	syncVal2 : function (srcObject, dstObject){
	  
	  srcObject = document.getElementsByName(srcObject);
	  srcObject = srcObject[0];
	  
	  dstObject = document.getElementsByName(dstObject);
	  dstObject = dstObject[0];
	  
	  /**
	  * First check all the source and destinated value
	  * That they are numeric and valid
	  *
	  */
	  if(!isNumeric(srcObject.value)){
	    
	    srcObject.value = 0;
	  }
	  
	  if(!isNumeric(dstObject.value)){
	    
	    dstObject.value = 0;
	  }
	  
	  /**
	  * Sync the val now
	  *
	  */
	  dstObject.value = parseFloat(dstObject.value) - parseFloat(srcObject.value);
	},
	
	/**
	* sync the val with 2 parameters take place
	*
	* @param  integer   srcObject
	* @param  String    dstObject
	*
	*/
	syncVal2F : function (srcObject, dstObject){
	  
	  srcObject = document.getElementsByName(srcObject);
	  srcObject = srcObject[0];
	  
	  dstObject = document.getElementsByName(dstObject);
	  dstObject = dstObject[0];
	  
	  /**
	  * First check all the source and destinated value
	  * That they are numeric and valid
	  *
	  */
	  if(!isNumeric(srcObject.value)){
	    
	    srcObject.value = 0;
	  }
	  
	  if(!isNumeric(dstObject.value)){
	    
	    dstObject.value = 0;
	  }
	  
	  /**
	  * Sync the val now
	  *
	  */
	  var tmpResult   = parseFloat(dstObject.value) - parseFloat(srcObject.value);
	  dstObject.value = tmpResult.toFixed(2);
	},
	
	/**
	* Sum the val with 2 parameters take place
	*
	* @param  integer   srcObject
	* @param  String    dstObject
	*
	*/
	sumVal2 : function (srcObject, dstObject){
	  
	  srcObject = document.getElementsByName(srcObject);
	  srcObject = srcObject[0];
	  
	  dstObject = document.getElementsByName(dstObject);
	  dstObject = dstObject[0];
	  
	  /**
	  * First check all the source and destinated value
	  * That they are numeric and valid
	  *
	  */
	  if(!isNumeric(srcObject.value)){
	    
	    srcObject.value = 0;
	  }
	  
	  if(!isNumeric(dstObject.value)){
	    
	    dstObject.value = 0;
	  }
	  
	  /**
	  * sum the val now
	  *
	  */
	  dstObject.value = parseFloat(dstObject.value) + parseFloat(srcObject.value);
	},
	
	/**
	* Sum the val with 2 parameters take place
	* with floating point, 2 decimal
	*
	* @param  integer   srcObject
	* @param  String    dstObject
	*
	*/
	sumVal2F : function (srcObject, dstObject){
	  
	  srcObject = document.getElementsByName(srcObject);
	  srcObject = srcObject[0];
	  
	  dstObject = document.getElementsByName(dstObject);
	  dstObject = dstObject[0];
	  
	  /**
	  * First check all the source and destinated value
	  * That they are numeric and valid
	  *
	  */
	  if(!isNumeric(srcObject.value)){
	    
	    srcObject.value = 0;
	  }
	  
	  if(!isNumeric(dstObject.value)){
	    
	    dstObject.value = 0;
	  }
	  
	  /**
	  * sum the val now
	  *
	  */
	  var tmpResult   = parseFloat(dstObject.value) + parseFloat(srcObject.value);
	  dstObject.value = tmpResult.toFixed(2);
	},
	
	/**
	* set this object with this value
	* This function will be used as 
	* variable content manipulator
	*
	* @param  String  dstObject
	* @param  String  thisValue
	*
	*/
	setThisObject : function (dstObject, thisValue){
	  
	  dstObject = document.getElementsByName(dstObject);
	  
	  if(dstObject.length > 0){
	    
	    dstObject = dstObject[0];
	  }
	  
	  dstObject.value = thisValue;
	},

  sayHello : function (){
  
	  alert("Linked GMisc.js Successfuly");
	}
}
